package com.easemob.chat;

import com.easemob.util.EMLog;
import com.xonami.javaBells.DefaultJingleSession.SessionState;
import com.xonami.javaBells.IceAgent;
import com.xonami.javaBells.StunTurnAddress;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import net.java.sip.communicator.impl.protocol.jabber.extensions.jingle.ContentPacketExtension;
import net.java.sip.communicator.impl.protocol.jabber.extensions.jingle.ContentPacketExtension.CreatorEnum;
import net.java.sip.communicator.impl.protocol.jabber.extensions.jingle.ContentPacketExtension.SendersEnum;
import net.java.sip.communicator.impl.protocol.jabber.extensions.jingle.JingleIQ;
import net.java.sip.communicator.impl.protocol.jabber.extensions.jingle.JinglePacketFactory;
import net.java.sip.communicator.impl.protocol.jabber.extensions.jingle.Reason;
import net.java.sip.communicator.impl.protocol.jabber.extensions.jingle.ReasonPacketExtension;
import org.jivesoftware.smack.XMPPConnection;

class EMReceiverJingleSession extends EMVoiceCallSession
{
  private static final String TAG = EMReceiverJingleSession.class.getSimpleName();
  private List<ContentPacketExtension> acceptedContent = null;
  private boolean isCallerUsingRelay = false;
  private boolean peerSendCallerRelay = false;

  public EMReceiverJingleSession(EMSessionHandler paramEMSessionHandler, String paramString, XMPPConnection paramXMPPConnection)
  {
    super(paramEMSessionHandler, paramString, paramXMPPConnection);
    this.callDirection = EMCallDirection.INCOMING;
  }

  protected void closeSession(Reason paramReason)
  {
    super.closeSession(paramReason);
  }

  public void handleCallerRelay(JingleIQ paramJingleIQ)
  {
    EMLog.d(TAG, "handleCallerRelay : " + paramJingleIQ.getReason().getText());
    this.peerSendCallerRelay = true;
    this.jingleIQ = paramJingleIQ;
    ack(this.jingleIQ);
    if (paramJingleIQ.getReason().getText().contains("enabled"))
    {
      this.isCallerUsingRelay = true;
      if (!this.selectionDone)
        return;
      if (!this.isLocalHostCandiate)
        connectPeerRelay();
    }
    else
    {
      if (!this.selectionDone)
        return;
      if (!this.isLocalHostCandiate)
        connectRelayServer();
    }
  }

  public void handleSessionAccept(JingleIQ paramJingleIQ)
  {
  }

  public void answerCall()
  {
    EMLog.d(TAG, "start answer call");
    if ((this.callState == EMCallStateChangeListener.CallState.CONNECTED) || (this.callState == EMCallStateChangeListener.CallState.ACCEPTED))
      return;
    EMLog.i(TAG, "Accepting incomig call!");
    JingleIQ localJingleIQ = JinglePacketFactory.createCallAccept(this.myJid, this.peerJid, this.sessionId);
    this.connection.sendPacket(localJingleIQ);
    this.jingleStreamManager.startStream(this.streamParams);
    changeState(EMCallStateChangeListener.CallState.CONNECTED, EMCallStateChangeListener.CallError.ERROR_NONE);
    changeState(EMCallStateChangeListener.CallState.ACCEPTED, EMCallStateChangeListener.CallError.ERROR_NONE);
  }

  private void acceptContent()
  {
    try
    {
      EMLog.i(TAG, "Accepting incomig jingle call!");
      StunTurnAddress localStunTurnAddress = StunTurnAddress.getAddress(this.serverAddress, this.serverAddress);
      this.iceAgent = new IceAgent(false, localStunTurnAddress);
      this.iceAgent.createStreams(this.jingleStreamManager.getMediaNames());
      this.iceAgent.addLocalCandidateToContents(this.acceptedContent);
      build(this.iceAgent);
      localJingleIQ = JinglePacketFactory.createSessionAccept(this.myJid, this.peerJid, this.sessionId, this.acceptedContent);
      this.connection.sendPacket(localJingleIQ);
      this.state = DefaultJingleSession.SessionState.NEGOTIATING_TRANSPORT;
      this.iceAgent.addRemoteCandidates(this.jingleIQ);
      this.iceAgent.startConnectivityEstablishment();
      changeState(EMCallStateChangeListener.CallState.CONNECTING, EMCallStateChangeListener.CallError.ERROR_NONE);
    }
    catch (IOException localIOException)
    {
      EMLog.w(TAG, "An error occured. Rejecting call!");
      localJingleIQ = JinglePacketFactory.createCancel(this.myJid, this.peerJid, this.sessionId);
      this.connection.sendPacket(localJingleIQ);
      closeSession(Reason.FAILED_APPLICATION);
      changeState(EMCallStateChangeListener.CallState.DISCONNNECTED, EMCallStateChangeListener.CallError.ERROR_TRANSPORT);
    }
    catch (IllegalArgumentException localIllegalArgumentException)
    {
      EMLog.w(TAG, "An error occured. Rejecting call!");
      JingleIQ localJingleIQ = JinglePacketFactory.createCancel(this.myJid, this.peerJid, this.sessionId);
      this.connection.sendPacket(localJingleIQ);
      closeSession(Reason.FAILED_APPLICATION);
      changeState(EMCallStateChangeListener.CallState.DISCONNNECTED, EMCallStateChangeListener.CallError.ERROR_TRANSPORT);
    }
  }

  public void rejectCall()
  {
    EMLog.i(TAG, "Rejecting call!");
    try
    {
      closeSession(Reason.DECLINE);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      EMLog.e(TAG, localException.getMessage());
    }
    changeState(EMCallStateChangeListener.CallState.DISCONNNECTED, EMCallStateChangeListener.CallError.ERROR_NONE);
  }

  void rejectSessionInitiate()
  {
    EMLog.d(TAG, "try to reject an incoming session initiate IQ request : from peer " + this.peerJid + " session id = " + this.sessionId);
    onBusy();
  }

  void acceptSessionInitiate()
  {
    EMLog.d(TAG, "accept an incoming session initiate request : from peer " + this.peerJid + " session id = " + this.sessionId);
    List localList = this.jingleIQ.getContentList();
    int i = 0;
    Iterator localIterator = localList.iterator();
    while (localIterator.hasNext())
    {
      ContentPacketExtension localContentPacketExtension = (ContentPacketExtension)localIterator.next();
      String str = localContentPacketExtension.getAttributeAsString("name");
      if ("video".equals(str))
      {
        i = 1;
        break;
      }
    }
    if (i == 0)
      this.jingleStreamManager = new EMJingleStreamManager(ContentPacketExtension.CreatorEnum.initiator);
    else
      this.jingleStreamManager = new EMJingleStreamManager(ContentPacketExtension.CreatorEnum.initiator, "video");
    try
    {
      this.acceptedContent = this.jingleStreamManager.parseIncomingAndBuildMedia(this.jingleIQ, ContentPacketExtension.SendersEnum.both);
      if (this.acceptedContent == null)
      {
        EMLog.i(TAG, "Rejecting call!");
        closeSession(Reason.INCOMPATIBLE_PARAMETERS);
        changeState(EMCallStateChangeListener.CallState.DISCONNNECTED, EMCallStateChangeListener.CallError.ERROR_NONE);
      }
      build(this.jingleStreamManager);
      acceptContent();
    }
    catch (IOException localIOException)
    {
      localIOException.printStackTrace();
      EMLog.d(TAG, localIOException.getMessage());
    }
  }

  public void handleSessionInitiate(JingleIQ paramJingleIQ)
  {
    EMLog.d(TAG, "call from : " + paramJingleIQ + " is ringing!");
    this.jingleIQ = paramJingleIQ;
    ack(this.jingleIQ);
    this.peerJid = paramJingleIQ.getFrom();
    EMVoiceCallManager.getInstance().onJingleInitiateAction(this);
  }

  public void handleSessionTerminate(JingleIQ paramJingleIQ)
  {
    this.callState = EMCallStateChangeListener.CallState.DISCONNNECTED;
    super.handleSessionTerminate(paramJingleIQ);
    changeState(EMCallStateChangeListener.CallState.DISCONNNECTED, EMCallStateChangeListener.CallError.ERROR_NONE);
  }

  public void onConnectionConnected()
  {
    EMVoiceCallManager.getInstance().onCallRinging(this);
  }

  private boolean acceptCallFrom(String paramString)
  {
    return true;
  }

  protected void onRelayCandidateSelected()
  {
    EMLog.d(TAG, "onRelayCandidateSelected was called");
    if ((this.peerSendCallerRelay) && (this.callState != EMCallStateChangeListener.CallState.CONNECTED))
      if (this.isCallerUsingRelay)
      {
        EMLog.d(TAG, "remote peer is relay type! and we try to connect to different peer");
        if (!this.isLocalHostCandiate)
          connectPeerRelay();
      }
      else
      {
        connectPeer();
      }
  }

  protected void onLocalCandidateSelected()
  {
    connectPeer();
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.EMReceiverJingleSession
 * JD-Core Version:    0.6.1
 */